package myutil;
/*
    date：
    topic：
    
*/

import java.sql.*;

public class DBUtil {
    private final static String driver="com.mysql.jdbc.Driver";
    private final static String url = "jdbc:mysql://localhost/web_face?characterEncoding=utf8&useSSL=false";
    private final static String user = "root";
    private final static String password  ="root";
    public static Connection getConn() {
        Connection connection = null;
        try {
            Class.forName(driver);
            connection=DriverManager.getConnection(url, user,
                    password);
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动加载失败");
            e.printStackTrace();
        }catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }
        return connection;
    }

    public static void close (Connection connection ,Statement
            statement , ResultSet resultSet) {
        try {
            if(connection!=null)connection.close();
            if(statement!=null)statement.close();
            if(resultSet!=null)resultSet.close();
        } catch (SQLException e) {
            System.out.println("数据库关闭异常");
            e.printStackTrace();
        }
    }

    //测试数据库连接是否成功
    public static void main(String []args) {
        Connection connection=null;
        connection = getConn();
        System.out.println(connection);
        close(connection, null, null);

    }
}

